using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameOverController : MonoBehaviour
{
    [Header("UI References")]
    [SerializeField] private Text wavesCompletedText;
    [SerializeField] private Text currentWaveText;
    [SerializeField] private Text zombiesKilledText;

    [Header("Animation")]
    [SerializeField] private Image animationImage;
    [SerializeField] private Sprite[] animationSprites;
    [SerializeField] private float frameRate = 0.1f;

    private void Start()
    {
        Time.timeScale = 1f;

        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;

        StartCoroutine(PlayAnimation());
        DisplayStats();
    }

    private IEnumerator PlayAnimation()
    {
        if (animationImage == null || animationSprites == null || animationSprites.Length == 0)
            yield break;

        foreach (Sprite sprite in animationSprites)
        {
            animationImage.sprite = sprite;
            yield return new WaitForSeconds(frameRate);
        }

        animationImage.sprite = animationSprites[animationSprites.Length - 1];
    }

    private void DisplayStats()
    {
        int wavesCompleted = GameOverManager.GetLastWavesCompleted();
        int currentWave = GameOverManager.GetLastCurrentWave();
        int zombiesKilled = GameOverManager.GetLastZombiesKilled();

        if (wavesCompletedText != null)
        {
            wavesCompletedText.text = $"Waves Completed: {wavesCompleted}";
        }

        if (currentWaveText != null)
        {
            currentWaveText.text = $"Last Wave: {currentWave}";
        }

        if (zombiesKilledText != null)
        {
            zombiesKilledText.text = $"Zombies Killed: {zombiesKilled}";
        }
    }
}